
package top.yiciyuan.web.servlet;


import java.io.InputStream;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


public abstract class BaseBackServlet extends HttpServlet {

	
	public void service(HttpServletRequest request, HttpServletResponse response) {
		try {
			
			/*借助反射，调用对应的方法*/
			String method = (String) request.getParameter("method");
			System.out.println("BaseBackServlet: method="+method);
			Method m = this.getClass().getMethod(method, javax.servlet.http.HttpServletRequest.class,
					javax.servlet.http.HttpServletResponse.class);
			String redirect = m.invoke(this,request, response).toString();
			System.out.println(redirect);
			/*根据方法的返回值，进行相应的客户端跳转，服务端跳转，或者仅仅是输出字符串*/
			if(redirect.startsWith("@"))//重定向r
				response.sendRedirect(redirect.substring(1));
			else if(redirect.startsWith("%"))//转发请求的结果
				request.getRequestDispatcher(redirect.substring(1)).forward(request, response);
			else//异步
				response.getWriter().print(redirect);
				
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}


